<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Captcha;
class Login extends Controller
{
    public function index()
    {
        return view();
    }
    public function dologin()
    {

        if(request()->IsAjax()){
            $username = input("post.username");
            $password = input("post.password");
            $captcha = input("post.captcha");
            if(!captcha_check($captcha)){
                //验证失败
                return json_encode(array('status'=>'n','info'=>'验证码错误'));

            };
            $hasUser = Db::name('admin')->where(array('username'=>$username))->find();
            if($hasUser){
                if($hasUser['password'] == md5($password)){
                    session('username',$username);
                    session('id',$hasUser['id']);
                    return json_encode(array('status'=>'y','info'=>'登录成功!'));

                }else{
                    return json_encode(array('status'=>'n','info'=>'用户名或者密码错误'));
                }
            }else{
                return json_encode(array('status'=>'n','info'=>'管理员不存在'));
            }
        }
    }
    public function loginout(){
        session(null);
        if(!session('?username')){
            $this->success('退出登录成功','Login/index');
        }else{
            $this->error('退出登录失败','Admin/index');
        }
    }
}